package com.hqj.controller;

import com.alibaba.fastjson.JSON;
import com.hqj.entity.Homes;
import com.hqj.service.HomesServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hc")
public class HomesController extends BaseController<Homes> {

    @Resource(name="homesServiceI")
    private HomesServiceI hsi;

    @Override
    public String index() {
        return "pages/homes/index";
    }

    @Override
    public String addEdit(HttpServletRequest req, Model model) {
        String id = req.getParameter("id");//获取前端传递的id值
        Homes home = new Homes();
        if(id!=null){
            home.setId(Integer.parseInt(id));
            home = hsi.get(home);
            //把查询到的值传递到跳转的页面
        }
        model.addAttribute("home",home);
        return "pages/homes/addEdit";
    }

    @Override
    public String find(Homes homes) {
        Map<String,Object> data = new HashMap<>();
        List<Homes> list = hsi.find(homes);
        data.put("code",0);
        data.put("msg","");
        data.put("count",list.size());
        data.put("data",list);
        return JSON.toJSONString(data);
    }

    @Override
    public Map<String, Object> add(Homes homes) {
        return hsi.add(homes);
    }

    @Override
    public Map<String, Object> edit(Homes homes) {
        return hsi.edit(homes);
    }

    @Override
    public Map<String, Object> remove(Homes homes) {
        return hsi.remove(homes.getId());
    }
}
